package com.yidiandao.business.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.constant.Common;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 发布求购信息
 */
public class AddPictureGridAdapter extends BaseAdapter {
    Context context;
    List<String> list;
    private boolean mShowAdd;//是否显示添加图片按钮

    public AddPictureGridAdapter(Context context, List<String> list, boolean showAdd) {
        super();
        this.context = context;
        this.list = list;
        mShowAdd = showAdd;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (list.size() == 0)
            count = 2;
        else if (list.size() > 0 && list.size() < Common.PICTURESIZE && mShowAdd)
            count = list.size() + 1;
        else if (list.size() == Common.PICTURESIZE)
            count = Common.PICTURESIZE;
        else
            count = list.size();
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView =
                    LayoutInflater.from(context).inflate(R.layout.item_add_picture, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position >= list.size() && mShowAdd) {
            holder.mTvTip.setVisibility(View.GONE);
            holder.image.setVisibility(View.VISIBLE);
            ImageLoader.loadLocalImage(context, holder.image, R.mipmap.ic_add_picture);
            if (list.size() == 0 && position == 1) {
                holder.image.setVisibility(View.GONE);
                holder.mTvTip.setVisibility(View.VISIBLE);
                holder.mTvTip.setText(Html.fromHtml(UIUtils.getString(R.string.ask_to_buy_pic_tip)));
            }

        } else {
            holder.mTvTip.setVisibility(View.GONE);
            holder.image.setVisibility(View.VISIBLE);
            ImageLoader.loadImage(context, holder.image, list.get(position));
        }

        return convertView;
    }

    public class ViewHolder {
        public ImageView image;
        private TextView mTvTip;

        public ViewHolder(View view) {
            image = (ImageView) view.findViewById(R.id.iv_item_pic);
            mTvTip = (TextView) view.findViewById(R.id.tv_tip);

        }
    }
}
